#include <iostream>
#include <memory>
#include <vector>

using std::cout;
using std::endl;
using std::shared_ptr;
using std::vector;

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy )
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    void print() const
    {
        cout << "(" << _ix 
             << ", " << _iy
             << ")" << endl;
    }

    ~Point()
    {
        cout << "~Point()" << endl;
    }

private:
    int _ix;
    int _iy;
};

void test()
{
    shared_ptr<int> sp(new int(10));
    cout << "*sp = " << *sp << endl;
    cout << "sp.get() = " << sp.get() << endl;
    cout << "sp.use_count() = " << sp.use_count() << endl;

    cout << endl << endl;
    shared_ptr<int> sp2 = sp;
    cout << "*sp2 = " << *sp2 << endl;
    cout << "*sp = " << *sp << endl;
    cout << "sp2.get() = " << sp2.get() << endl;
    cout << "sp.get() = " << sp.get() << endl;
    cout << "sp.use_count() = " << sp.use_count() << endl;
    cout << "sp2.use_count() = " << sp2.use_count() << endl;

    cout << endl << endl;
    shared_ptr<int> sp3(new int(200));
    cout << "*sp3 = " << *sp3 << endl;
    cout << "sp3.get() = " << sp3.get() << endl;
    cout << "sp3.use_count() = " << sp3.use_count() << endl;

    cout << endl;
    sp3 = sp;//赋值
    cout << "*sp3 = " << *sp3 << endl;
    cout << "*sp = " << *sp << endl;
    cout << "sp3.get() = " << sp3.get() << endl;
    cout << "sp.get() = " << sp.get() << endl;
    cout << "sp.use_count() = " << sp.use_count() << endl;
    cout << "sp3.use_count() = " << sp3.use_count() << endl;

    cout << endl << "shared_ptr作为容器元素" << endl;
    vector<shared_ptr<int>> vec;
    vec.push_back(sp);
    vec.push_back(std::move(sp));
    vec.push_back(std::move(sp3));
    vec.push_back(shared_ptr<int>(new int(100)));
}

void test2()
{
    shared_ptr<Point> sp(new Point(1, 2));
    vector<shared_ptr<Point>> vec;
    vec.push_back(std::move(sp));//要执行shared_ptr移动构造函数
    vec.push_back(shared_ptr<Point>(new Point(100)));

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

